package com.xiyu.service.service.elder;

import com.xiyu.service.model.elder.info.ElderInfo;
import com.xiyu.service.model.elder.info.ElderInfoDraft;
import com.xiyu.service.repository.elder.info.ElderInfoRepository;
import com.xiyu.service.service.infra.data.DictNoService;
import com.xiyu.service.vo.elder.elderInfo.ElderInfoGetOutput;
import com.xiyu.service.vo.elder.elderInfo.ElderInfoUpdateInput;
import com.xiyu.service.vo.elder.elderInfo.ElderInfoInsertInput;
import com.xiyu.service.vo.elder.elderInfo.ElderInfoQueryOutput;
import com.xiyu.service.vo.elder.elderInfo.ElderInfoQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.elder.ElderInfoConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.elder.ElderInfoErrorCode.*;

/**
 * 老人资料 Service 实现类
 */
@Service
@Validated
public class ElderInfoServiceImpl implements ElderInfoService {

    @Resource
    private ElderInfoRepository elderInfoRepository;

    @Resource
    private DictNoService dictNoService;

    @Override
    public PageResult<ElderInfoQueryOutput> query(ElderInfoQueryInput inputVO) {
        Page<ElderInfo> pageInfo = elderInfoRepository.query(inputVO);
        List<ElderInfoQueryOutput> listInfo = ElderInfoConvert.INSTANCE.queryPageOutputConvert(pageInfo.getRows());
        return new PageResult<>(listInfo,pageInfo.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(ElderInfoInsertInput inputVO) {
        ElderInfo elderInfo = ElderInfoConvert.INSTANCE.insertInputConvert(inputVO);
        elderInfo = ElderInfoDraft.$.produce(elderInfo, draft -> {
            draft.setElderNo(dictNoService.produceNo("elder"));
        });
        ElderInfo newElderInfo = elderInfoRepository.insert(elderInfo);
        return newElderInfo.id();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long update(ElderInfoUpdateInput inputVO) {
        ElderInfo elderInfo = ElderInfoConvert.INSTANCE.updateInputConvert(inputVO);
        Optional<ElderInfo> opOldElderInfo = elderInfoRepository.findById(elderInfo.id());
        if(!opOldElderInfo.isPresent())
            throw exception(ELDER_INFO_NOT_EXISTS);
        else
            elderInfoRepository.update(elderInfo);
        return elderInfo.id();
    }

    @Override
    public ElderInfoGetOutput get(Long id) {
        Optional<ElderInfo> opElderInfo = elderInfoRepository.findById(id);
        if(!opElderInfo.isPresent())
            throw exception(ELDER_INFO_NOT_EXISTS);
        else {
            return ElderInfoConvert.INSTANCE.getOutputConvert(opElderInfo.get());
        }
    }

}
